package org.bongo.open.cumulo.arbitrator.broker;

import org.bongo.open.cumulo.arbitrator.broker.exception.CumuloBrokerClientException;
import org.bongo.open.cumulo.core.context.CumuloContext;
import org.bongo.open.cumulo.core.message.MessageUnit;
import org.bongo.open.cumulo.core.utils.LazyIndexedObjectPool;
import org.bongo.open.cumulo.procengine.broker.ProcEngineBroker;
import org.bongo.open.cumulo.procengine.broker.ProcEnginePartitioner;
import org.bongo.open.cumulo.procengine.broker.RoundRobinPartitioner;

public class ProcEngineBrokerClient {

	private ProcEnginePartitioner p;
	private LazyIndexedObjectPool<ProcEngineBroker> brokerPool;
	private int distributionIndex;
	
	public ProcEngineBrokerClient() {
		p = new RoundRobinPartitioner();
		
	}
	

	public void submitMessage(MessageUnit<?> message, CumuloContext context) throws CumuloBrokerClientException {
		if(brokerPool == null)
			brokerPool = new LazyIndexedObjectPool<ProcEngineBroker>(new ProcEngineBrokerFactory(context));
		
		ProcEngineBroker broker = brokerPool.obtainFromPool(distributionIndex);
		broker.messageSubmitCallback(message, p);
	}
	
	public void queryBrokerForStatus() {
		
	}

	
	/**
	 * this sets the broker agent to be connected to
	 * @param distributionIndex
	 */
	public void setDistributionIndex(int distributionIndex) {
		this.distributionIndex = distributionIndex;
				
	}


	public void shutdown() {
		brokerPool.evictAll();
		
	}

	
}
